package 其他工具类;

import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class zip {
    public static void main(String[] args) {
        String targetPath = "D:\\Win10Driver\\OneDrive\\桌面\\xxx";
        String zipFile = "D:\\Win10Driver\\OneDrive\\桌面\\人才租房补贴申请材料模版.zip";
//        unzipFile(targetPath, zipFile);
        generateFile(targetPath);
    }

    public static void generateFile(String path) {
        File file = new File(path);
        path = file.getAbsolutePath();
        if (!file.exists()) {
            return;
        }
        try (FileOutputStream fos = new FileOutputStream(path + ".zip");
             ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(fos))) {
            zipFile(zipOutputStream, file, "");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void zipFile(ZipOutputStream zipOutputStream, File file, String dir) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            dir = dir.length() == 0 ? "" : dir + File.separator;
            for (File fi : files) {
                zipFile(zipOutputStream, fi, dir + fi.getName());
            }
        } else {
            try (FileInputStream fis = new FileInputStream(file)) {
                zipOutputStream.putNextEntry(new ZipEntry(dir));
                int len = 0;
                byte[] bytes = new byte[20480];
                while ((len = fis.read(bytes)) > 0) {
                    zipOutputStream.write(bytes, 0, len);
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 解压文件
     *
     * @param targetPath  解压后目录
     * @param zipFilePath zip文件
     */
    public static void unzipFile(String targetPath, String zipFilePath) {

        byte[] buffer = new byte[40960];

        try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath), Charset.forName("GBK"))) {

            ZipEntry zipEntry;
            while ((zipEntry = zis.getNextEntry()) != null) {
                String fileName = zipEntry.getName();
                File newFile = new File(targetPath + File.separator + fileName);
                // 创建目录
                if (zipEntry.isDirectory()) {
                    newFile.mkdirs();
                } else {
                    // 创建父目录（如果不存在）
                    new File(newFile.getParent()).mkdirs();

                    // 解压文件
                    try (FileOutputStream fos = new FileOutputStream(newFile)) {
                        int length;
                        while ((length = zis.read(buffer)) > 0) {
                            fos.write(buffer, 0, length);
                        }
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
